package io.renren.modules.school.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.school.dao.SchoolDao;
import io.renren.modules.school.entity.*;
import io.renren.modules.school.service.ProvinceAndCitiesService;
import io.renren.modules.school.service.SchoolService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("schoolService")
public class SchoolServiceImpl extends ServiceImpl<SchoolDao, SchoolEntity> implements SchoolService {

    @Autowired
    SchoolDao schoolDao;
    @Autowired
    ProvinceAndCitiesService provinceAndCitiesService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String province= (String) params.get("q[province]");
        String region= (String) params.get("q[region]");
        String cities= (String) params.get("q[city]");
        String nature= (String) params.get("q[nature]");
        String level= (String) params.get("q[level]");
        Page<SchoolEntity> page = this.selectPage(
                new Query<SchoolEntity>(params).getPage(),
                new EntityWrapper<SchoolEntity>()
                       .eq(StringUtils.isNotBlank(nature),"nature",nature)
                        .eq(StringUtils.isNotBlank(level),"level",level)
                        .like(StringUtils.isNotBlank(province),"province",province)
                        .like(StringUtils.isNotBlank(region),"region",region)
                        .like(StringUtils.isNotBlank(cities),"cities",cities)
        );

        return new PageUtils(page);
    }

    @Override
    public List<TreeEntity> getSchoolTree() {
//        List<SchoolEntity> schoolList=schoolDao.getSchoolList();
//        List<TreeEntity> AreaList=schoolDao.getAreaList();
//        List<TreeEntity> list=new ArrayList<>();
//        for(TreeEntity treeEntity:AreaList){
//            treeEntity.setOpen(false);
//            list.add(treeEntity);
//            for(SchoolEntity schoolEntity:schoolList){
//                if(treeEntity.getName().equals(schoolEntity.getArea())){
//                    TreeEntity t=new TreeEntity();
//                    t.setId(schoolEntity.getId());
//                    t.setName(schoolEntity.getCode()+" "+schoolEntity.getName());
//                    t.setParentId(treeEntity.getId());
//                    list.add(t);
//                }
//            }
//        }
//        return list;
        int j=0;
        List<SchoolEntity> schoolList=schoolDao.getSchoolTreeList();
        List<TreeEntity> AreaList=provinceAndCitiesService.getAreaList();
        List<ProvinceAndCitiesEntity> provinceList=provinceAndCitiesService.getProvincesList();
        List<TreeEntity> list=new ArrayList<>();
        for(TreeEntity area:AreaList){
            list.add(area);
            for(ProvinceAndCitiesEntity province:provinceList){
                    if(province.getRegionId().equals(area.getId())){
                        TreeEntity pTree=new TreeEntity();
                        pTree.setId(province.getProvinceId());
                        pTree.setName(province.getProvince());
                        pTree.setParentId(area.getId());
                    list.add(pTree);

                    String[] cities=province.getCities().split(",");
                    for(int i=0;i<cities.length;i++) {
                        TreeEntity tCity=new TreeEntity();
                        tCity.setId((10000000+j));
                        j++;
                        tCity.setName(cities[i]);
                        tCity.setParentId(province.getProvinceId());
                        list.add(tCity);
                        for (SchoolEntity school : schoolList) {
                            if(school.getCities()!=null) {
                                if (school.getCities().contains(cities[i])) {
                                    TreeEntity s = new TreeEntity();
                                    s.setId(school.getId());
                                    s.setName(school.getName());
                                    s.setParentId(tCity.getId());
                                    list.add(s);
                                }
                            }
                        }
                    }
                }
            }



        }


        return list;
    }

    @Override
    public PageUtils getListBySchoolId(Integer id) {
        PageUtils page=new PageUtils();
        page.setList(schoolDao.getListBySchoolId(id));
        ;
        return page;
    }

    @Override
    public void insertLowScore(LowscoreEntity school) {
        schoolDao.insertLowScore(school);
    }

    @Override
    public LowscoreEntity getLowscoreEntityByYear(LowscoreEntity school) {
        return schoolDao.getLowscoreEntityByYear(school);
    }

    @Override
    public void deleteLowScore(Integer id) {
        schoolDao.deleteLowScore(id);
    }

    @Override
    public List getUniversitys(Map<String, Object> params) {
        String wenlike= (String) params.get("idWenlike");
        String learnType= (String) params.get("idLearnType");
        String level=(String)params.get("idLevel");
        String wl=wenlike+learnType+level;
        if(level.equals("本科")){
            // params.remove("idLevel");
            params.put("noInLevel","专科");
        }else{
            //params.remove("idLevel");
            params.put("noInLevel","本科");
        }

        switch (wl){
            case "理科普通本科":params.put("predictor","putonglike");break;
            case "理科校企合作本科":params.put("predictor","xiaoqihezuolike");break;
            case "理科中外合作本科":params.put("predictor","zhongwaihezuolike");break;
            case "理科3+2连读本科":params.put("predictor","liandulike");break;
            case "理科春考本科":params.put("predictor","chunkaolike");break;
            case "理科艺术本科":params.put("predictor","yishulike");break;
            case "文科普通本科":params.put("predictor","putongwenke");break;
            case "文科校企合作本科":params.put("predictor","xiaoqihezuowenke");break;
            case "文科中外合作本科":params.put("predictor","zhongwaihezuowenke");break;
            case "文科3+2连读本科":params.put("predictor","lianduwenke");break;
            case "文科春考本科":params.put("predictor","chunkaowenke");break;
            case "文科艺术本科":params.put("predictor","yishuwenke");break;
            case "理科普通专科":params.put("predictor","zhuankeputonglike");break;
            case "理科校企合作专科":params.put("predictor","zhuankexiaoqilike");break;
            case "理科中外合作专科":params.put("predictor","zhuankezhongwaihezuolike");break;
            case "理科3+2连读专科":params.put("predictor","zhuankeliandulike");break;
            case "理科春考专科":params.put("predictor","zhuankechunkaolike");break;
            case "理科艺术专科":params.put("predictor","zhuankeyishulike");break;
            case "文科普通专科":params.put("predictor","zhuankeputongwenke");break;
            case "文科校企合作专科":params.put("predictor","zhuankexiaoqiwenke");break;
            case "文科中外合作专科":params.put("predictor","zhuankezhongwaihezuowenke");break;
            case "文科3+2连读专科":params.put("predictor","zhuankelianduwenke");break;
            case "文科春考专科":params.put("predictor","zhuankechunkaowenke");break;
            case "文科艺术专科":params.put("predictor","zhuankeyishuwenke");break;

        }
           List<UniversityEntity> l=schoolDao.getUniversitys(params);
        return l;
    }

    @Override
    public List getLastYearScore(Map<String, Object> params) {
        String wenlike= (String) params.get("idWenlike");
        String learnType= (String) params.get("idLearnType");
        String level=(String)params.get("idLevel");
        String wl=wenlike+learnType+level;
        if(level.equals("本科")){
            // params.remove("idLevel");
            params.put("noInLevel","专科");
        }else{
            //params.remove("idLevel");
            params.put("noInLevel","本科");
        }

        switch (wl){
            case "理科普通本科":params.put("predictor","putonglike");break;
            case "理科校企合作本科":params.put("predictor","xiaoqihezuolike");break;
            case "理科中外合作本科":params.put("predictor","zhongwaihezuolike");break;
            case "理科3+2连读本科":params.put("predictor","liandulike");break;
            case "理科春考本科":params.put("predictor","chunkaolike");break;
            case "理科艺术本科":params.put("predictor","yishulike");break;
            case "文科普通本科":params.put("predictor","putongwenke");break;
            case "文科校企合作本科":params.put("predictor","xiaoqihezuowenke");break;
            case "文科中外合作本科":params.put("predictor","zhongwaihezuowenke");break;
            case "文科3+2连读本科":params.put("predictor","lianduwenke");break;
            case "文科春考本科":params.put("predictor","chunkaowenke");break;
            case "文科艺术本科":params.put("predictor","yishuwenke");break;

            case "理科普通专科":params.put("predictor","zhuankeputonglike");break;
            case "理科校企合作专科":params.put("predictor","zhuankexiaoqilike");break;
            case "理科中外合作专科":params.put("predictor","zhuankezhongwaihezuolike");break;
            case "理科3+2连读专科":params.put("predictor","zhuankeliandulike");break;
            case "理科春考专科":params.put("predictor","zhuankechunkaolike");break;
            case "理科艺术专科":params.put("predictor","zhuankeyishulike");break;
            case "文科普通专科":params.put("predictor","zhuankeputongwenke");break;
            case "文科校企合作专科":params.put("predictor","zhuankexiaoqiwenke");break;
            case "文科中外合作专科":params.put("predictor","zhuankezhongwaihezuowenke");break;
            case "文科3+2连读专科":params.put("predictor","zhuankelianduwenke");break;
            case "文科春考专科":params.put("predictor","zhuankechunkaowenke");break;
            case "文科艺术专科":params.put("predictor","zhuankeyishuwenke");break;

        }


        return schoolDao.getLastYearScore(params);
    }

    @Override
    public void changeALL(Map<String, Object> params) {
        String wenlike= (String) params.get("wenlike");
        Float f= Float.parseFloat((String) params.get("score"));
        //Date data= new  Date();
        List<SchoolPredictor> l=schoolDao.getSchoolPredictorList();
        if(wenlike.equals("wenke")){
            for(SchoolPredictor s:l)
            {
                LowscoreEntity le=schoolDao.getLowscoreBySchoolId(s.getId());
                try{
                    if(le!=null) {
                        if (le.getChunkaowenke() != null) s.setChunkaowenke(le.getChunkaowenke() + f);
                        if (le.getPutongwenke() != null) s.setPutongwenke(le.getPutongwenke() + f);
                        if (le.getLianduwenke() != null) s.setLianduwenke(le.getLianduwenke() + f);
                        if (le.getXiaoqihezuowenke() != null) s.setXiaoqihezuowenke(le.getXiaoqihezuowenke() + f);
                        if (le.getYishuwenke() != null) s.setYishuwenke(le.getYishuwenke() + f);
                        if (le.getZhongwaihezuowenke() != null) s.setZhongwaihezuowenke(le.getZhongwaihezuowenke() + f);
                        if (le.getZhuankeputongwenke() != null) s.setZhuankeputongwenke(le.getZhuankeputongwenke() + f);
                        if (le.getZhuankexiaoqiwenke() != null) s.setZhuankexiaoqiwenke(le.getZhuankexiaoqiwenke() + f);
                        if (le.getZhuankezhongwaihezuowenke() != null)
                            s.setZhuankezhongwaihezuowenke(le.getZhuankezhongwaihezuowenke() + f);
                        if (le.getZhuankelianduwenke() != null) s.setZhuankelianduwenke(le.getZhuankelianduwenke() + f);
                        if (le.getZhuankeyishuwenke() != null) s.setZhuankeyishuwenke(le.getZhuankeyishuwenke() + f);
                        if (le.getZhuankechunkaowenke() != null)
                            s.setZhuankechunkaowenke(le.getZhuankechunkaowenke() + f);
                        schoolDao.updateSchoolPredictor(s);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        }else{
            for(SchoolPredictor s:l)
            {
                LowscoreEntity le=schoolDao.getLowscoreBySchoolId(s.getId());
                try{

                    if(le!=null) {
                        if (le.getChunkaolike() != null) s.setChunkaolike(le.getChunkaolike() + f);
                        if (le.getPutonglike() != null) s.setPutonglike(le.getPutonglike() + f);
                        if (le.getLiandulike() != null) s.setLiandulike(le.getLiandulike() + f);
                        if (le.getXiaoqihezuolike() != null) s.setXiaoqihezuolike(le.getXiaoqihezuolike() + f);
                        if (le.getYishulike() != null) s.setYishulike(le.getYishulike() + f);
                        if (le.getZhongwaihezuolike() != null) s.setZhongwaihezuolike(le.getZhongwaihezuolike() + f);
                        if(le.getZhuankeputonglike()!=null) s.setZhuankeputonglike(le.getZhuankeputonglike()+f);
                        if(le.getZhuankexiaoqilike()!=null) s.setZhuankexiaoqilike(le.getZhuankexiaoqilike()+f);
                        if(le.getZhuankezhongwaihezuolike()!=null) s.setZhuankezhongwaihezuolike(le.getZhuankezhongwaihezuolike()+f);

                        if(le.getZhuankeliandulike()!=null) s.setZhuankeliandulike(le.getZhuankeliandulike()+f);
                        if(le.getZhuankeyishulike()!=null) s.setZhuankeyishulike(le.getZhuankeyishulike()+f);
                        if(le.getZhuankechunkaolike()!=null) s.setZhuankechunkaolike(le.getZhuankechunkaolike()+f);
                        schoolDao.updateSchoolPredictor(s);
                    }

                }catch (Exception e){
                    e.printStackTrace();
                }

            }

        }
    }

    @Override
    public void setLastThreeYear(String[] year) {
        List<UniversityEntity> l=schoolDao.getAllUniversitys();
        String years="";
        for(int i=0;i<year.length;i++){
            years=years+year[i]+",";
        }
        years=years.substring(0,years.length()-1);
        for(UniversityEntity u:l){
            Map map=new HashMap();
            List<LowscoreEntity> lastThreeYears=schoolDao.getLastYearScoreBySchoolId(years,u.getId());
            String lty="";
//            for(LastThreeYear lastThreeYear:lastThreeYears){
//                lty=lty+lastThreeYear.getYear()+" : "+lastThreeYear.getPredictor()+"分; ";
//            }
           // u.setLastThreeYear(lty);
        }

    }


}
